package com.fmatos.controller;

import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import freemarker.template.TemplateException;

@Configuration
public class MvcConfigurer extends WebMvcConfigurerAdapter {
	@Bean
	public ViewResolver viewResolver() {
		FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
		resolver.setCache(true);
		resolver.setSuffix(".ftl");
		resolver.setContentType("text/html; charset=UTF-8");
		
		return resolver;
	}

	@Bean
	public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
//		Reload FreeMarker templates without restarting the container
//		If you are using FreeMarker, then set spring.freemarker.cache to false. See FreeMarkerAutoConfiguration for other FreeMarker customization options.
		FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
				factory.setTemplateLoaderPath("classpath:templates");
				factory.setDefaultEncoding("UTF-8");
				FreeMarkerConfigurer result = new FreeMarkerConfigurer();
				result.setConfiguration(factory.createConfiguration());
				return result;
	}
	
}